/*
 * Created on Sat Sep 16 2017
 * 
 * Copyright (c) 2017 jianglinjie
 */
'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _NetRequest2 = require('./NetRequest');

var _NetRequest3 = _interopRequireDefault(_NetRequest2);

var _HttpMethod = require('../constants/HttpMethod');

var HttpMethod = _interopRequireWildcard(_HttpMethod);

var _ResponseType = require('../constants/ResponseType');

var ResponseType = _interopRequireWildcard(_ResponseType);

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var FormRequest = function (_NetRequest) {
  _inherits(FormRequest, _NetRequest);

  function FormRequest(url, urlParam, formParam) {
    var responseType = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : ResponseType.JSON_TYPE;

    _classCallCheck(this, FormRequest);

    var form = '';

    if (formParam) {
      var _iteratorNormalCompletion = true;
      var _didIteratorError = false;
      var _iteratorError = undefined;

      try {
        for (var _iterator = Object.keys(formParam)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
          var key = _step.value;

          var pair = encodeURIComponent(key) + '=' + encodeURIComponent(formParam[key]);
          if (form.length) {
            form = form + '&';
          }

          form = form + pair;
        }
      } catch (err) {
        _didIteratorError = true;
        _iteratorError = err;
      } finally {
        try {
          if (!_iteratorNormalCompletion && _iterator.return) {
            _iterator.return();
          }
        } finally {
          if (_didIteratorError) {
            throw _iteratorError;
          }
        }
      }
    }

    return _possibleConstructorReturn(this, (FormRequest.__proto__ || Object.getPrototypeOf(FormRequest)).call(this, url, urlParam, form || null, { 'Content-Type': 'application/x-www-form-urlencoded' }, HttpMethod.POST, responseType));
  }

  return FormRequest;
}(_NetRequest3.default);

exports.default = FormRequest;